import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userData: {},
    pageY:''
  },
  mutations: {

    async getData(state){
      if(localStorage.getItem('id')&&localStorage.getItem('token')){
        const {data:res} = await Vue.prototype.$http.get(`/user/${localStorage.getItem('id')}`)
        state.userData = res[0]
        console.log(state.userData);
      }
    },
    // 修改图片
    setHeader(state,url){
      state.userData.user_img = url
      this.commit('setHeaderData')
    },
    //修改昵称
    setName(state,name){
      state.userData.name = name
      this.commit('setHeaderData')
    },
    //修改个性签名
    setMessage(state,message){
      state.userData.user_desc = message
      this.commit('setHeaderData')
    },
    //选择性别
    chooseSex(state,sex){
      state.userData.gender = sex
      this.commit('setHeaderData')
    },
    //出生日期
    chooseDate(state,date){
      state.userData.password = date
      this.commit('setHeaderData')
      state.shut = 1
    },
    //保存pageY
    getPageY(state,pageY){
      state.pageY = pageY
    },
    async setHeaderData(state){
      const {data:res} = await Vue.prototype.$http.post(`/update/${localStorage.getItem('id')}`,state.userData)
      if (res.code !== 200) return Vue.prototype.$toast.fail(res.msg)
      Vue.prototype.$toast.success('修改成功')
    }
  },
  actions: {

  },
  modules: {
  }
})
